﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bmz.Runtime.Session
{
    /// <summary>
    /// Extension methods for <see cref="IBmzSession"/>.
    /// </summary>
    public static class BmzSessionExtensions
    {
        /// <summary>
        /// Gets current User's Id.
        /// Throws <see cref="BmzException"/> if <see cref="IBmzSession.UserId"/> is null.
        /// </summary>
        /// <param name="session">Session object.</param>
        /// <returns>Current User's Id.</returns>
        public static long GetUserId(this IBmzSession session)
        {
            if (!session.UserId.HasValue)
            {
                throw new BmzException("Session.UserId is null! Probably, user is not logged in.");
            }

            return session.UserId.Value;
        }

        /// <summary>
        /// Gets current Tenant's Id.
        /// Throws <see cref="BmzException"/> if <see cref="IBmzSession.TenantId"/> is null.
        /// </summary>
        /// <param name="session">Session object.</param>
        /// <returns>Current Tenant's Id.</returns>
        /// <exception cref="BmzException"></exception>
        public static int GetTenantId(this IBmzSession session)
        {
            if (!session.TenantId.HasValue)
            {
                throw new BmzException("Session.TenantId is null! Possible problems: User is not logged in, current user in not tenant user or this is not a multi-tenant application.");
            }

            return session.TenantId.Value;
        }
    }
}
